package com.oig.sys.workflow.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.workflow.dao.ITbListenerEventDao;
import com.oig.sys.workflow.dto.ListenerEventDto;
import com.oig.sys.workflow.entity.TbListenerEvent;
import com.oig.sys.workflow.entity.TbListenerObject;
import com.oig.sys.workflow.service.ITbAutoRuleService;
import com.oig.sys.workflow.service.ITbListenerEventService;
import com.oig.sys.workflow.vo.ListenerEventVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbListenerEventServiceImpl implements ITbListenerEventService {

	private final ITbListenerEventDao tbListenerEventDao;
	private final ITbAutoRuleService autoRuleService;

	@Autowired
	public TbListenerEventServiceImpl(ITbListenerEventDao tbListenerEventDao,
									  ITbAutoRuleService autoRuleService){
		 this.tbListenerEventDao = tbListenerEventDao;
		 this.autoRuleService = autoRuleService;
	}

	private ListenerEventVo addListenerEvent(ListenerEventDto eventDto){
		TbListenerEvent listenerEvent = new TbListenerEvent();
		BeanUtils.copyProperties(eventDto, listenerEvent,"id");
		listenerEvent.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
		this.tbListenerEventDao.save(listenerEvent);
		return this.buildVo(listenerEvent);
	}

	private ListenerEventVo updateListenerEvent(ListenerEventDto eventDto){
		TbListenerEvent listenerEvent = this.tbListenerEventDao.getLEById(eventDto.getId());
		if (listenerEvent==null){
			throw new BadRequestException("记录不存在") ;
		}
		if (!listenerEvent.getObjectId().equals(eventDto.getObjectId())){
			throw new BadRequestException("明细id传错") ;
		}
		BeanUtils.copyProperties(eventDto, listenerEvent);
		this.tbListenerEventDao.save(listenerEvent);
		return this.buildVo(listenerEvent);
	}

	private ListenerEventVo buildVo(TbListenerEvent listenerEvent){
		ListenerEventVo eventVo = new ListenerEventVo();
		BeanUtils.copyProperties(listenerEvent, eventVo);
		return eventVo;
	}

	@Transactional
	@Override
	public List<ListenerEventVo> addListenerEventList(TbListenerObject listenerObject, List<ListenerEventDto> listenerEventDtoList) {
		List<ListenerEventVo> eventVoList = new ArrayList<>();
		for (ListenerEventDto eventDto : listenerEventDtoList){
			eventDto.setObjectId(listenerObject.getId());
			eventVoList.add(this.addListenerEvent(eventDto));
		}
		return eventVoList;
	}

	@Transactional
	@Override
	public List<ListenerEventVo> updateListenerEventList(TbListenerObject listenerObject, List<ListenerEventDto> listenerEventDtoList) {
		List<ListenerEventVo> eventVoList = new ArrayList<>();
		List<Integer> idList = new ArrayList<>();
		for (ListenerEventDto eventDto : listenerEventDtoList){
			eventDto.setObjectId(listenerObject.getId());
			ListenerEventVo eventVo ;
			if (eventDto.getId()==null){
				eventVo = this.addListenerEvent(eventDto);
			} else {
				eventVo = this.updateListenerEvent(eventDto);
			}
			idList.add(eventVo.getId());
			eventVoList.add(eventVo);
		}
		List<Integer> delIdList = this.tbListenerEventDao.findAllDelId(listenerObject.getId(), idList);
		if (CollectionUtil.isNotEmpty(delIdList)){
			//如果rule里面已经引用了这个监听，删除掉是否bug？
			List<Integer> ruleIdList = this.autoRuleService.findRuleIdByEventIdList(delIdList);
			if (CollectionUtil.isNotEmpty(ruleIdList)){
				throw new BadRequestException("删除了已有自动化规则的监听事件") ;
			}
			tbListenerEventDao.deleteByIdList(delIdList);
		}
		return eventVoList;
	}


	@Override
	public List<ListenerEventVo> findAllByObjectId(Integer objectId) {
		List<TbListenerEvent> listenerEvents = this.tbListenerEventDao.findAllByObjectId(objectId);
		List<ListenerEventVo> eventVoList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(listenerEvents)){
			log.debug("object:{},all lister event:{}",objectId,listenerEvents);
			listenerEvents.forEach((event)->eventVoList.add(this.buildVo(event)));
		}
		return eventVoList;
	}

	@Transactional
	@Override
	public void delAllByObjectId(Integer objectId) {
		List<Integer> delIdList = this.tbListenerEventDao.findAllIdByObjectId(objectId) ;
		if (CollectionUtil.isNotEmpty(delIdList)){
			tbListenerEventDao.deleteByIdList(delIdList);
		}
	}
}
